package com.example.elm.Account

import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.activity.ComponentActivity
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.elm.Public.AccountInfo
import com.example.elm.R
import com.example.elm.databinding.FoodCardBinding

private val name = ArrayList<String>()
private val money = ArrayList<String>()
class FoodcardActivity : ComponentActivity() {
    private lateinit var binding: FoodCardBinding
    private lateinit var adapter: FoodcardAdapter
    private var AccountInformation: AccountInfo? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = FoodCardBinding.inflate(layoutInflater)
        setContentView(binding.root)
        AccountInformation = CommonUtil.getSerializable(this, "info", AccountInfo::class.java)
        init_Foodcard()

        val layoutManager = LinearLayoutManager(this)
        binding.foodcardUi.layoutManager = layoutManager
        adapter = FoodcardAdapter(name,money)
        binding.foodcardUi.adapter = adapter

        binding.back.setOnClickListener {
            val intent = Intent()
            intent.putExtra("info", AccountInformation)
            setResult(RESULT_OK, intent)
            finish()
        }
    }

    private fun init_Foodcard() {
        money.clear()
        money.add("18")
        name.add("3元x8张，24元超级吃货卡")
        money.add("18")
        name.add("5元x6张，30元超级吃货卡")
        money.add("10")
        name.add("5元x4张，24元超级吃货卡")
        money.add("28")
        name.add("5元x4张，20元超级吃货卡-3个月")
        money.add("108")
        name.add("5元x4张，24元超级吃货卡-12个月")
        money.add("30")
        name.add("5元x8张，40元超级吃货卡")
    }

    private class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val foodcard_Amount: TextView = view.findViewById(R.id.foodcard_Amount)
        val foodcard_name: TextView = view.findViewById(R.id.foodcard_name)
    }
    private class FoodcardAdapter(val name: ArrayList<String>,val money: ArrayList<String>): RecyclerView.Adapter<ViewHolder>(){
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
            val view = LayoutInflater.from(parent.context)
                .inflate(R.layout.foodcard_ui, parent, false)
            return ViewHolder(view)
        }

        override fun onBindViewHolder(holder: ViewHolder, position: Int) {
            val information1 = name[position]
            val information2 = money[position]
            holder.foodcard_name.text = information1
            holder.foodcard_Amount.text = information2
        }

        override fun getItemCount(): Int {
            return name.size
        }
    }
}